
FIUMAPS.LocationsByCategory = { category_id: null, services_ul: null,

    getPointsByCategoryID: function(category_id) {
        // Retrieve all services by category. 
        //show modal loading screen
        if (category_id !== this.category_id) {
            FIUMAPS.ModalWindow.show($('MapLoading'));

            // clear map overlays
            ClearOverlays();
            this.ResetCategoryStyles();

            this.category_id = category_id;

            if (FIUMAPS.Cache.isCategoryLoaded(this.category_id)) {
                //already in cache. pull from it.
                this.LoadCategoryListFromCache();
            } else {
                this.loadServices();
                // register that this category has been loaded into cache.  
                FIUMAPS.Cache.setCategoryLoaded(this.category_id)
            }
        }
    },
    Reset: function() {
        // clear map overlays
        ClearOverlays();
        this.ResetCategoryStyles();
        accordionResize('Categories_Div');
    },
    loadServices: function() {
        // Query Task
        var qtask = new esri.arcgis.gmaps.QueryTask("http://maps.fiu.edu/arcgis/rest/services/FIUCampusMaps/ServicesNBuildings/MapServer/0");
        // Query
        var query = new esri.arcgis.gmaps.Query();

        // set query parameters. 
        query.returnGeometry = true;
        query.where = "CAMPUS_ID = " + FIUMAPS.CampusInfo.getCurrentCampusID() + " AND CATEGORY_ID = " + this.category_id;
        query.outFields = ["SERVICE_ID", "NAME", "DESCRIPTION", "FLOOR", "ROOM_NUMBER", "CATEGORY_ID", "CATEGORY_NAME", "BUILDING_CODE", "BUILDING_NAME", "BUILDING_ID", "CAMPUS_ID", "CAMPUS_CODE", "CAMPUS_NAME"];
        // execute query task
        qtask.execute(query, false, FIUMAPS.LocationsByCategory._CallBack);
    },
    ResetCategoryStyles: function() {
        // remove the 'active' class of the currently active category (actually all tabs)
        $$('#Categories_Div li').each(function(li, index) {
            li.removeClass('active');
            //console.debug(li.id);
            // remove all child elements except the first link.
            if (li.hasChildNodes()) {
                while (li.childNodes.length > 1) {
                    li.removeChild(li.lastChild);

                }
            }
        });
    },
    LoadCategoryListFromCache: function() {
        var cat_li_id = "CategoryID_" + this.category_id;
        //Create UL html element that will be holding category results
        this.services_ul = document.createElement('ul');

        this.hasServicesHeader = false;

        var arrLocationCache = FIUMAPS.Cache.Data[FIUMAPS.CampusInfo.getCurrentCampusCode()].Locations;
        //loop through cache looking for Category.
        for (var i = 0; i < arrLocationCache.length; i++) {
            if (arrLocationCache[i].Attributes.CATEGORY_ID == this.category_id) { // found category.
                this.createHTMLList(arrLocationCache[i].Geometries, arrLocationCache[i].Attributes);
            }
        }

        // set the clicked tab to 'active'
        $(cat_li_id).addClass('active');

        $(cat_li_id).appendChild(this.services_ul);

        accordionResize('Categories_Div');

        //hide modal window.
        FIUMAPS.ModalWindow.hide();
    },
    createHTMLList: function(geometry, attributes) {

        // create html list item element
        var li = document.createElement('li');
        // create html link item element
        var link = this.CreateLinkElement(attributes.NAME);

        addPointListeners(geometry, attributes, link);

        // add link to list item container
        li.appendChild(link);
        // append list item to unordered list.
        this.services_ul.appendChild(li);

    },
    CreateLinkElement: function(label) {
        // create html link element 
        var link = document.createElement('a');
        link.style.cursor = 'pointer';
        link.style.marginBottom = '5px';
        link.title = label;
        link.appendChild(document.createTextNode(label));
        return link;
    },
    _CallBack: function(fset) {

        if (fset !== null) {

            // Keep track of services as to not duplicate them. 

            var results = fset.features, attributes, geometry;

            //loop through features  
            for (var i = 0; i < results.length; i++) {
                attributes = results[i].attributes;
                geometry = results[i].geometry;

                var letter = attributes.NAME.capitalize().substr(0, 1);
                var item = new FIUMAPS.Cache.LocationCacheItem(FIUMAPS.LocationTypes.SERVICE, letter, attributes, geometry);
                //add to cache.
                FIUMAPS.Cache.AddLocationCacheItem(item);
            }

            FIUMAPS.LocationsByCategory.LoadCategoryListFromCache();

        }
    }
}
